package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.training.domain.Scheme;
import com.weishi.project.training.domain.SchemeDetails;
import com.weishi.project.training.mapper.SchemeMapper;
import com.weishi.project.training.service.ISchemeDetailsService;
import com.weishi.project.training.service.ISchemeService;
import com.weishi.project.training.service.ITrainingDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 训练方案 主表 Service业务层处理
 */
@Service
public class SchemeServiceImpl extends ServiceImpl<SchemeMapper, Scheme> implements ISchemeService {

    @Autowired
    private SchemeMapper schemeMapper;

    /**
     * 训练方案子表接口
     */
    @Autowired
    private ISchemeDetailsService schemeDetailsService;


    /**
     * 训练课程接口
     */
    @Autowired
    private ITrainingDayService trainingDayService;



    /**
     * 查询训练方案列表
     */
    @Override
    public List<Scheme> selectSchemeList(Scheme scheme) {
        return schemeMapper.selectSchemeList(scheme);
    }



    /**
     * 教练员端 -- 查询训练方案
     */
    @Override
    public AjaxResult getSchemeList(Scheme scheme) {
        return AjaxResult.success(list(Wrappers.<Scheme>lambdaQuery()
                .eq(Scheme::getTrainingMode , scheme.getTrainingMode())
                .eq(Scheme::getTargetValue , scheme.getTargetValue())
                .eq(Scheme::getGroupNumber , scheme.getGroupNumber())
                .eq(Scheme::getSecondLevel , scheme.getSecondLevel())
                .eq(Scheme::getFirstLevel , scheme.getFirstLevel())));
    }



    /**
     * 新增训练方案 和 组
     */
    @Override
    public AjaxResult add(Scheme scheme) {
        // 新增方案数据
        scheme.setCtime(new Date());
        save(scheme);

        // 新增方案数据组
        if(scheme.getSchemeDetailsList().size() > 0){
            scheme.getSchemeDetailsList().forEach( p-> {
                p.setTblSchemeId(scheme.getId());
                schemeDetailsService.save(p);
            });
        }
        return AjaxResult.success();
    }



    /**
     * 修改训练方案 和 组
     */
    @Override
    public AjaxResult edit(Scheme scheme) {
        // 修改方案数据
        updateById(scheme);

        // 修改方案组数据
        if(scheme.getSchemeDetailsList().size() > 0){
            scheme.getSchemeDetailsList().forEach( p-> {
                p.setId(p.getId());
                schemeDetailsService.updateById(p);
            });
        }
        return AjaxResult.success();
    }



    /**
     * 获取训练方案详细信息
     */
    @Override
    public AjaxResult schemeById(Long id) {
        Scheme scheme = getById(id);
        List<SchemeDetails> schemeDetailsList = schemeDetailsService.list(Wrappers.<SchemeDetails>lambdaQuery().eq(SchemeDetails::getTblSchemeId , id));
        if(schemeDetailsList.size() > 0){
            scheme.setSchemeDetailsList(schemeDetailsList);
        }
        return AjaxResult.success(scheme);
    }









}